//
//  KPNetTestTableViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/3/29.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import Alamofire

class KPNetTestTableViewController: KPBaseTableViewController {

    var titles = ["简单数据-GET", "复杂数据-GET", "简单数据-POST", "复杂数据-POST", "上传数据","天气数据"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return titles.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = titles[indexPath.row]
        KPLog(baseURL)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0:
            Api.getJSONRequest(.getSimple, parameters: nil)
                .mapBaseModel()
                .subscribe(onNext: { (model) in
                    KPLog(model)
                }, onError: { (e) in
                    KPLog(e)
                })
                .disposed(by: bag)
        case 1:
            Api.getJSONRequest(.getComplex, parameters: ["参数": "我是参数"])
                .mapObject(NetTestModel.self)
                .subscribe(onNext: { (model) in
                    KPLog(model)
                })
                .disposed(by: bag)
        case 2:
            Api.postJSONRequest(.postSimple, parameters: ["参数": "我是参数"])
                .mapBaseModel()
                .subscribe(onNext: { (model) in
                    KPLog(model)
                })
                .disposed(by: bag)
        case 3:
            Api.postJSONRequest(.postComplex, parameters: ["参数": "我是参数"])
                .mapObject(NetTestModel.self)
                .subscribe(onNext: { (model) in
                    KPLog(model)
                })
                .disposed(by: bag)
        case 4:
            Api.uploadRequest(.uploadTest, parameters: ["参数": "我是参数"], images: [(#imageLiteral(resourceName: "默认头像"),"file1"), (#imageLiteral(resourceName: "默认头像"),"file2"), (#imageLiteral(resourceName: "默认头像"),"file3")])
                .mapBaseModel()
                .subscribe(onNext: { (model) in
                    KPLog(model)
                }, onError: { (e) in
                    KPLog(e)
                })
                .disposed(by: bag)
        case 5:

            Alamofire.request(URL.init(string: "http://wthrcdn.etouch.cn/weather_mini")!, method: .get, parameters: nil)
                .response { (respon) in
                    KPLog(respon)
            }

//            Api.getJSONRequest(.weatherTest, parameters: nil)
//                .mapBaseModel()
//                .subscribe(onNext: { (model) in
//                    KPLog(model)
//                }, onError: { (e) in
//                    KPLog(e)
//                })
//            .disposed(by: bag)
        default:
            break
        }
    }

}
